use hypers_core::prelude::*;
use hypers_core::tracing::info;

pub async fn hello(req: Request) -> String {
    let method = req.method().to_string();
    let uri = req.uri().to_string();
    format!("method = {}, uri = {}",method,uri)
}

struct Basic;
#[controller(name = "basic")]
impl Basic {
    // http://127.0.0.1:7878/basic/header
    // Request Header Params
    #[get("/header")]
    pub async fn header(host: Header<String>, accept: Header<String>) -> impl Responder {
        (200,format!("host = {}, accept = {}",host,accept))
    }
    // http://127.0.0.1:7878/basic/admin@outlook.com/18
    // Uri Path Params
    #[delete("/:email/:age")]
    pub async fn delete(email: Path<String>, age: Path<u16>) -> impl Responder {
        (200,format!("email = {}, age = {}",email,age))
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt().init();
    let mut root = Router::default();
    root.get("/hello", hello); // http://127.0.0.1:7878/hello
    root.push(Basic);

    info!("root = {:#?}", root);
    let listener = hypers_core::TcpListener::bind("127.0.0.1:7878").await?;
    hypers_core::listen(root, listener).await
}
